import axios from "axios";
import { Message } from "element-ui";
import router from "@/router";

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
});
service.defaults.withCredentials = true;

service.interceptors.request.use(
  function (config) {
    config.headers = {
      ...config.headers,
    };
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  function (response) {
    // return response.data; //先不走code判断

    if (response.config.responseType == "blob") {
      return response.data;
    }

    if (response.data.status != 200) {
      Message({
        message: response.data.resultValue || `错误码${response.data.status}`,
        type: "error",
      });
      return Promise.reject(response.data.resultValue || `错误码${response.data.status}`);
    } else {
      return response.data;
    }
  },
  function (error) {
    Message({
      message: "登录失效",
      type: "error",
    });
    router.push("/login");
    return Promise.reject(error);
  }
);
export default service;
